<template>
  <div class="user_wrap">
    <BreadCrumb crumb="用户管理"></BreadCrumb>
    <div class="top_bar">
      <el-row>
        <el-col :span="8">
          <el-input
            placeholder="请输入搜索内容"
            v-model="keyword"
            class="input-with-select"
            size="small"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="fnGetData(1)"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="8" :offset="8"
          ><AddUser @fnSetKey="fnGetKey" class="pull-right"></AddUser
        ></el-col>
      </el-row>
    </div>
    <UserTable :users="aUserList"></UserTable>
    <el-pagination
      background
      layout="prev, pager, next"
      :total=total
      :page-size=pagesize
      :current-page="page"
      style="text-align: center; margin-top: 10px"
      @current-change="fnGetPage"
    >
    </el-pagination>
  </div>
</template>

<script>
import BreadCrumb from "@/components/widget/BreadCrumb";
import AddUser from "@/components/widget/AddUser";
import UserTable from "@/components/widget/UserTable";
import cons from "@/components/constant";

export default {
  name: "User",
  data() {
    return {
      keyword: "",
      page: 1,
      total:0, // 总条目数
      pagesize:2, // 定义变量限制一页显示的条数
      aUserList: [],
    };
  },
  components: {
    BreadCrumb,
    AddUser,
    UserTable,
  },
  mounted() {
    this.fnGetData(1);
  },
  methods: {
    // 搜索, 方法一
    // GetData:function(){
    //    let token = localStorage.token;
    //   this.axios
    //     .get(cons.apis + "/users/search/", {
    //       headers: {
    //         Authorization: "JWT " + token,
    //       },
    //       responseType: "json",
    //       params: {
    //         keyword: this.keyword,
    //       },
    //     })
    //     .then((dat) => {
    //       this.aUserList = dat.data;
    //     })
    //     .catch((err) => {
    //       console.log(err);
    //     });
    // },
    // 获取全部的用户信息
    fnGetData: function (num) {
     
      let token = localStorage.token;
      this.axios
        .get(cons.apis + "/users/users/", {
          headers: {
            Authorization: "JWT " + token,
          },
          responseType: "json",
          // 查询字符串
          params: {
            page: num,
            pasize:this.pagesize,
            keyword: this.keyword,
          },
        })
        .then((dat) => {
          console.log('获取全部的用户信息',dat.data)
          this.aUserList = dat.data.results;      //分页 改
          // this.page = dat.data.page;
          // 总条目数
          this.total = dat.data.count;   //分页 改
        })
        .catch((err) => {
          console.log(err);
        });
    },
    // 点击添加用户时触发的方法
    fnGetKey: function (dat) {
      this.keyword = dat;
      this.fnGetData(1);
    },
    // 翻页触发的方法
    fnGetPage: function (dat) {
      // dat 是第几页的页数
      this.page = dat;
      this.fnGetData(this.page);
    },
  },
};
</script>

<style scoped>
.top_bar {
  width: 95.2%;
  overflow: hidden;
  margin: 10px auto 10px;
}
</style>
